<template>
  <div class="portal-container">
    <app-header />
    
    <main class="portal-content">
      <div class="content-grid">
        <!-- 左侧栏 -->
        <div class="left-column">
          <user-panel @show-avatar-dialog="showAvatarDialog" />
          <applications />
        </div>
        
        <!-- 中间栏 -->
        <div class="middle-column">
          <aiRequest />
          <AiRepairRequest />
          
          <!-- 日程详情悬浮层 -->
          <calendar-detail
            v-if="showCalendarDetail"
            :events="calendarEvents"
            @close="showCalendarDetail = false"
            @add-event="handleAddEvent"
          />
        </div>
        
        <!-- 右侧栏 -->
        <div class="right-column">
          <calendar 
            @show-add-schedule="showAddScheduleDialog"
            @show-detail="showCalendarDetail = true"
          />
          <my-apps />
        </div>
      </div>
    </main>
    
    <app-footer />
    
    <!-- 头像上传对话框 -->
    <el-dialog title="修改头像" :visible.sync="avatarDialogVisible" width="400px">
      <div class="avatar-upload">
        <el-upload
          class="avatar-uploader"
          action="/api/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <i v-if="!imageUrl" class="el-icon-plus avatar-uploader-icon"></i>
          <i v-else class="el-icon-picture avatar-uploader-icon"></i>
        </el-upload>
        <div class="avatar-tips">建议尺寸：200x200像素，大小不超过2MB</div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="avatarDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveAvatar">确 定</el-button>
      </span>
    </el-dialog>
    
    <!-- 新增日程对话框 -->
    <el-dialog title="新增日程" :visible.sync="scheduleDialogVisible" width="500px">
      <el-form :model="scheduleForm" label-width="80px">
        <el-form-item label="活动内容">
          <el-input 
            type="textarea" 
            v-model="scheduleForm.content" 
            :autosize="{ minRows: 3, maxRows: 5 }"
            placeholder="请输入活动内容">
          </el-input>
        </el-form-item>
        <el-form-item label="开始时间">
          <el-date-picker
            v-model="scheduleForm.startTime"
            type="datetime"
            placeholder="选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间">
          <el-date-picker
            v-model="scheduleForm.endTime"
            type="datetime"
            placeholder="选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="全天事件">
          <el-switch v-model="scheduleForm.isAllDay"></el-switch>
        </el-form-item>
        <el-form-item label="提醒">
          <el-select v-model="scheduleForm.reminder" placeholder="请选择提醒时间">
            <el-option label="不提醒" value="none"></el-option>
            <el-option label="活动开始时" value="start"></el-option>
            <el-option label="5分钟前" value="5m"></el-option>
            <el-option label="15分钟前" value="15m"></el-option>
            <el-option label="30分钟前" value="30m"></el-option>
            <el-option label="1小时前" value="1h"></el-option>
            <el-option label="1天前" value="1d"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="scheduleDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveSchedule">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import AppHeader from '@/components/gate/AppHeader.vue'
import AppFooter from '@/components/AppFooter.vue'
import UserPanel from '@/components/gate/UserPanel.vue'
import Applications from '@/components/gate/Applications.vue'
import Messages from '@/components/gate/Messages.vue'
import Calendar from '@/components/gate/Calendar.vue'
import MyApps from '@/components/gate/MyApps.vue'
import TaskList from '@/components/gate/TakeList.vue'
import CalendarDetail from '@/components/gate/CalendarDetail.vue'
import AiRequest from '@/components/gate/AiRequest.vue'
import AiRepairRequest from '@/components/gate/AiRepairRequest.vue'

export default {
  name: 'Home',
  components: {
    AppHeader,
    AppFooter,
    UserPanel,
    Applications,
    Messages,
    Calendar,
    MyApps,
    TaskList,
    CalendarDetail,
    AiRequest,
    AiRepairRequest
  },
  data() {
    return {
      avatarDialogVisible: false,
      imageUrl: '',
      scheduleDialogVisible: false,
      scheduleForm: {
        content: '',
        startTime: '',
        endTime: '',
        isAllDay: false,
        reminder: '5m'
      },
      showCalendarDetail: false,
      calendarEvents: [
        { id: 1, date: '2023-06-15', time: '09:00', content: '部门例会' },
        { id: 2, date: '2023-06-15', time: '14:00', content: '客户拜访' },
        { id: 3, date: '2023-06-16', time: '10:30', content: '项目评审' }
      ]
    }
  },
  methods: {
    showAvatarDialog() {
      this.avatarDialogVisible = true
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG/PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    saveAvatar() {
      this.$message.success('头像上传成功')
      this.avatarDialogVisible = false
    },
    showAddScheduleDialog() {
      this.scheduleDialogVisible = true
    },
    saveSchedule() {
      const newEvent = {
        id: Date.now(),
        date: this.scheduleForm.startTime ? 
          new Date(this.scheduleForm.startTime).toISOString().split('T')[0] : 
          new Date().toISOString().split('T')[0],
        time: this.scheduleForm.isAllDay ? '全天' : 
          (this.scheduleForm.startTime ? 
           new Date(this.scheduleForm.startTime).toTimeString().substr(0,5) : ''),
        content: this.scheduleForm.content
      }
      
      this.calendarEvents.push(newEvent)
      this.$message.success('日程添加成功')
      this.scheduleDialogVisible = false
    },
    handleAddEvent(newEvent) {
      this.calendarEvents.push(newEvent)
    }
  }
}

</script>

<style scoped>
.portal-container {
  background-color: #f0f2f5;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.portal-content {
  flex: 1;
  padding: 80px 0 40px;
}

.content-grid {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 20px;
  display: grid;
  grid-template-columns: 350px 1fr 350px;
  gap: 20px;
}

.left-column, .right-column {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.middle-column {
  display: flex;
  flex-direction: column;
  gap: 20px;
  position: relative; /* 为悬浮层提供定位上下文 */
}

.avatar-upload {
  text-align: center;
}

.avatar-uploader >>> .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 150px;
  height: 150px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
}

.avatar-uploader >>> .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
}

.avatar-tips {
  margin-top: 10px;
  font-size: 12px;
  color: #999;
  text-align: center;
}


</style>